package com.dyz.leetcode;

public class Convert6 {
    public String convert(String s, int numRows) {
        if(s==null || s.length()<=1){return s;}

        char[] chars = s.toCharArray();
        int len = chars.length;

        //构建stringBuilder
        StringBuilder[] sbs = new StringBuilder[numRows];
        //初始化StringBuilder
        for(int i=0; i<numRows; i++){
            sbs[i] = new StringBuilder();
        }
        //
        for(int i=0; i<len;){
            // up to down
            for(int row=0; row<numRows && i<len; row++){
                sbs[row].append(chars[i]);
                i++;
            }

            //down to up
            for(int row=numRows-2; row>0 && i<len; row--){
                sbs[row].append(chars[i]);
                i++;
            }
        }
        //合并
        StringBuilder res = new StringBuilder();
        for(StringBuilder sb:sbs){
            res.append(sb);
        }
        return res.toString();

    }
}
